﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Money.Interfaces;
using Money.Data;
using One.Framework;
using One.Framework.Security;
using Money.Entities;
using MyLinqHelper;

namespace Money.Services
{
    public class UserService : IUserService
    {
        IUserDAO dao;

        public UserService(IUserDAO dao)
        {
            this.dao = dao;
        }

        public void CreateUser(User item)
        {
            try
            {
                dao.CreateUser(item);
            }
            catch(Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot create user");
            }
        }

        public void DeleteUser(int id)
        {
            try
            {
                dao.DeleteUser(id);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot delete user");
            }
        }

        public void UpdateUser(User item)
        {
            try
            {
                dao.UpdateUser(item);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot update user");
            }
        }

        public User Authentication(string username, string password)
        {
            try
            {
                Criteria c = new Criteria();
                c.Add(new Criterion(Comparision.Equal, UserOrderBy.Username.ToString(), username));
                c.Add(new Criterion(Comparision.Equal, UserOrderBy.Password.ToString(), EncryptUtility.HashMD5(password)));
                return dao.GetUser(c);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get user");
            }
        }

        public User GetUserById(int id)
        {
            try
            {
                return dao.GetUserById(id);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get user");
            }
        }

        public List<User> GetUsers(string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                return dao.GetUsers(orderBy, orderDirection, page, pageSize, out totalRecords);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get users");
            }
        }

        public User GetUserByUsername(string username)
        {
            try
            {
                Criteria c = new Criteria();
                c.Add(new Criterion(Comparision.Equal, UserOrderBy.Username.ToString(), username));
                return dao.GetUser(c);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get user");
            }
        }

        public User GetUserByEmail(string email)
        {
            try
            {
                Criteria c = new Criteria();
                c.Add(new Criterion(Comparision.Equal, UserOrderBy.Email.ToString(), email));
                return dao.GetUser(c);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get user");
            }
        }
    }
}
